Write some monitoring scripts in Perl and put them in crontab to schedule execution. It is sometimes found that a script runs too long and runs multiple instances at the same time, so it is necessary to control the script and run only one instance.
The simplest natural idea is to check and create an empty lock file in the script and delete it at the end of the script. Determine whether the script is running by determining the existence of the file. However, there is a bug that if the script run
-triggered when someone kills a script process using kill
EXITExit-This is a pseudo signal that is triggered when the script exits normally or after set-e because of an error
When you use a lock file, you can write this:
Copy Code code as follows:
if [!-e $lockfile]; Then
Touch $lockfile
Critical-section
RM $lockfile
Else
echo "Critic
statements:Case $VARAIBLE inPAT1)Branch 1;;PAT2)Branch 2;;...*)Branch N;;EsacCase supports GLOB-style wildcard characters:*: Any character of any length;?: any single character;[]: Any single character in the range;A|b:a or B;Example: Write a service framework script;$lockfile, Value/var/lock/subsys/script_name(1) This script can accept start, stop, restart, status of one of the four parameters;(2) If the parameter is not four, then the use of help a
to a known state when the script encounters a problem, you are great. Fortunately, Bash provides a way to run a command or a function when bash receives a UNIX signal. You can use the Trap command.
Copy Code code as follows:
Trap command signal [signal ...]
You can link multiple signals (lists can be obtained using kill-l), but in order to clean up the mess, we use only three of them: Int,term and exit. You can use-as to get the traps back to its original state.
Signa
function codeCall after: give the function name and pass the parameters on demandDefine the method:(1) Function F_name {function body}(2) F_name () {function body}Call Function:F_name [Argu1, ARGU2, ...]Custom function state return value:return [#]0: Success1-255: FailureNote: When the function code executes, once the return is encountered, the function code terminates and the function returns;Example: Execute start,stop,restart to implement the file state judgment separately:The Start,stop,res
data in Expect_out (buffer) that contains send
I always thought that once the send is executed in expect, expect_out (buffer) is emptied until new data is filled in, which is exactly what is used in the expect statement. And today in debugging, I was surprised to find the problem, the original expect_out (buffer) will not be automatically emptied, and expect to the data is probably not what you really want, very likely is historical data. This issue is also noted in the article "Expect_out (buff
only use three of them: INT, TERM, and EXIT. You can use-as to restore traps to the initial state. Signal description INT Interrupt-the TERM Terminate is triggered when someone uses Ctrl-C to Terminate the script-the EXIT Exit is triggered when someone uses kill to kill the script process-this is a pseudo signal, it is triggered when the script exits normally or after set-e exits due to an error. when you use the lock file, you can write: if [! -E $ lockfil
-triggered when someone kills a script process using killEXITExit-This is a pseudo-signal that is triggered when the script exits normally or after a set-e because of an errorWhen you use the lock file, you can write this:if [!-e $lockfile]; ThenTouch $lockfileCritical-sectionRM $lockfileElseecho "Critical-section is already running"FiWhen the most important part (Critical-section) is running, what happens if the script process is killed? The lock fil
file::lockfile, File::P ID, Proc::P id::file , etc.
Here is an example of file::lockfile, which is very simple:
Here is the code snippet:
01
#!/usr/bin/perl-w
02
Usefile::lockfile;
03
# lock file is located in the/tmp directory, named Test_file_lock.lck
04
My $
complete the above functions, suchFile: lockfile,File: PID,Proc: PID: File.
The following is an example of file: lockfile, which is very simple:
Below isCodeFragment:
01
#! /Usr/bin/perl-W
02
UseFile: lockfile;
03
# The lock file is located in the/tmp directory named test_file_lock.lck
In some cases, a process can only generate one instance for execution. In a Unix environment, the file-and record-locking mechanisms are provided, and basic solutions for single-instance processes are provided.Assume that a process generates a file when it starts to run, and locks the entire file, and only one of these write locks can be generated.If the subsequent process tries to generate a write lock, it will fail. This implies that the previous instance is running.
The following method det
when the script Exits normally or after set-e exits due to an error. When you use the lock file, you can write: if [! -E $ lockfile]; thentouch $ lockfilecritical-sectionrm $ lockfileelseecho "critical-section is already running" fi when the most important part (critical-section) is running, what happens if the script process is killed? The lock file will be thrown there, and your script will no longer run until it is deleted. Solution: if [! -E $
once, how to handle 4) stop, delete/var/lock/subsys/script_name, and display "Stop complete" consider: if it has stopped beforehand, how to deal with it? 5) Restart, stop, then start to consider : If there is no start, how to handle 6) How is the Status,/var/lock/subsys/script_name file exists, then displays "Script_nameisrunning ..."/var/ Lock/subsys/script_name file does not exist, it displays "script_nameisstopped ..." Where: Script_name is the current script name. $0#!/bin/bash#chkconfig:-8
/shirdrn/article/details/71055392. Provide service startup scripts for MongoDB and startClick (here) to collapse or open
#vim/etc/init.d/mongod #内容如下
————————————————————————————————
#!/bin/bash
# Chkconfig:-90 11
# DESCRIPTION:MONGODB Server SysV Script
. /etc/rc.d/init.d/functions
if [-f/etc/sysconfig/mongod]; Then
. /etc/sysconfig/mongod
Fi
Mongod=/usr/local/mongodb/bin/mongod
Prog=mongod
Lockfile=/data/db/mongod.lock
, resulting in file corruption.
Here is the code I wrote for The Docker container to process the file system lock:
#! /Bin/bash
# The location the lock will be attempted in
LOCKROOT = "/minecraft/lock"
LOCKDIRNAME = "lock"
LOCKFILENAME = "mylock. lck"
# This is the command to run if we get the lock.
RUNCMD = "./start. sh"
# Number of seconds to consider the Lock stale, this cocould be application dependent.
LOCKTIMEOUT = 60
SLEEPLOOP = 30
LOCKDIR =$ {LOCKROOT}/$ {LOCKDIRNAME}
. please contact us if you have any questions");
}
2. initialize the information and enter the chat room for the first time.
If ($ action = "enter ")
{
//////////////// Call the js program that displays the home screen ////////////// //////
Print ("parent. write2 (); n ");
// The Speech Content. xx entered the chat room
$ Message = "$ name
Go to the chat room ". $ message." ". date (" m Month d h: I ")." script parent. add ('$ name ',
'$ Photo'); parent. write1 (); script
";
}
// Update the
mongod file in the/etc/init. d directory
The content is as follows:
#!/bin/sh##mongod - Startup script for mongod## chkconfig: - 85 15# description: Mongodb database.# processname: mongod# Source function library. /etc/rc.d/init.d/functions# things from mongod.conf get there by mongod reading it# OPTIONSOPTIONS=" -f /etc/mongod.conf"#mongodmongod="/root/mongodb/bin/mongod"lockfile=/var/lock/subsys/mongodstart(){ echo -n $"Starting mongod: " daemon
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.